//
//  ManuallyaddVC.m
//  ProjectforHDX
//
//  Created by yinda on 2018/7/17.
//  Copyright © 2018年 H.DX. All rights reserved.
//

#import "ManuallyaddVC.h"
#import "PlateModel.h"
#import "CacheVC.h"
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
@interface ManuallyaddVC ()<UITextFieldDelegate>
{
    UITextField *snCodeText;
    UITextField *platCodeText;
}
@property (nonatomic, strong) NSMutableArray *detailArr;  //确认存件的数组用于传递
@property (nonatomic, strong) NSMutableArray *platesArr;
@end

@implementation ManuallyaddVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    UIView *view = [[UIView alloc]init];
//    [self.view addSubview:view];
    
    self.labnav.text = [UtilCheck language:@"添加板件"];
    [self.labnav setFont:NavFontSize];
    [self.labnav setTextColor:[UIColor whiteColor]];
    
    self.detailArr = [NSMutableArray new];
    self.platesArr = [NSMutableArray new];
    UILabel *platcodeLabel = [[RZCustomLable alloc]init];
    platcodeLabel.textAlignment = NSTextAlignmentRight;
    platcodeLabel.frame = CGRectMake(25, 60, 80, 21);
    platcodeLabel.text = @"*板件号:";
    platcodeLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:platcodeLabel];
    
    platCodeText = [[UITextField alloc]init];
    platCodeText.delegate = self;
    platCodeText.frame = CGRectMake(130, 55, 180, 35);
    platCodeText.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:platCodeText];
    
    UILabel *snLabel = [[RZCustomLable alloc]init];
    snLabel.frame = CGRectMake(25, 110, 80, 21);
    snLabel.textAlignment = NSTextAlignmentRight;
    snLabel.font = [UIFont systemFontOfSize:16];
    snLabel.text = @"*序列号:";
    [self.view addSubview:snLabel];
    
    snCodeText = [[UITextField alloc]init];
    snCodeText.delegate = self;
    snCodeText.frame = CGRectMake(130, 105, 180, 35);
    snCodeText.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:snCodeText];
    
    UILabel *boxLabel = [[RZCustomLable alloc]init];
    boxLabel.textAlignment = NSTextAlignmentRight;
    boxLabel.frame = CGRectMake(25, 160, 80, 21);
    boxLabel.text = @"*箱号:";
    boxLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:boxLabel];
    
    UILabel *boxcontentLabel = [[RZCustomLable alloc]init];
    boxcontentLabel.frame = CGRectMake(130, 160, 180, 21);
    boxcontentLabel.text = self.boxcode;
    boxcontentLabel.textColor = [UIColor blueColor];
    boxcontentLabel.textAlignment = NSTextAlignmentLeft;
    boxcontentLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:boxcontentLabel];
    
    UILabel *numberLabel = [[RZCustomLable alloc]init];
    numberLabel.textAlignment = NSTextAlignmentRight;
    numberLabel.frame = CGRectMake(25, 210, 80, 21);
    numberLabel.text = @"*数量:";
    numberLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:numberLabel];
    
    UILabel *numbercontentLabel = [[RZCustomLable alloc]init];
    numbercontentLabel.textAlignment = NSTextAlignmentLeft;
    numbercontentLabel.frame = CGRectMake(130, 210, 180, 21);
    numbercontentLabel.text = @"*1";
    numbercontentLabel.textColor = [UIColor blueColor];
    numbercontentLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:numbercontentLabel];
    
    UIButton *surebutton = [UIButton buttonWithType:0];
    surebutton.frame = CGRectMake((self.view.frame.size.width-300)/2, 300, 300, 45);
    [surebutton setBackgroundImage:[UIImage imageNamed:@"确认存件"] forState:0];
    [surebutton setTitleColor:[UIColor whiteColor] forState:0];
    [surebutton setTitle:@"确定" forState:0];
    [surebutton addTarget:self action:@selector(inquirePlat) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surebutton];
//    view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

- (void)inquirePlat{
    if ([platCodeText.text isEqualToString:@""]||platCodeText.text==nil) {
        [Toolobject showAlterwithMessage:@"请输入板件号"];
        return;
    }
    if ([snCodeText.text isEqualToString:@""]||snCodeText.text==nil) {
        [Toolobject showAlterwithMessage:@"请输入序列号"];
        return;
    }
    if (self.checkArr.count>0&&![snCodeText.text isEqualToString:@"DUMMY"]) {
        for (NSDictionary *dic in self.checkArr) {
            if ([platCodeText.text containsString:dic[@"partCode"]]&&[snCodeText.text isEqualToString:dic[@"partSN"]]) {
                [Toolobject showAlterwithMessage:@"已经扫描过此板件"];
                return;
               }
            }
        }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    params[@"containerCode"] = self.house.houseId;
    params[@"code"] = [snCodeText.text uppercaseString];
    params[@"plateCode"] = [platCodeText.text uppercaseString];
    params[@"plateSN"] = [snCodeText.text uppercaseString];
    [params setObject:self.type forKey:@"type"];
    [params setObject:self.operationType forKey:@"operationType"];
    NSString *key = [THMangerTool getKey];
    [params setObject:key forKey:@"key"];
    [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
    [AFHttpTool postJsonDatawithUrl:@"getPartInfo1_1_2" parameters:[params mutableCopy] success:^(id responseObject) {
        
        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
                NSMutableDictionary *data = [responseObject objectForKey:@"data"];
                data[@"containerNo"] = self.boxcode;
//                [self.platesDicArr  replaceObjectAtIndex:index withObject:data];
                
                PlateModel *model = [[PlateModel alloc]init];
                model.containerNo = self.boxcode;
                model.sn = [data objectForKey:@"partSN"];
                model.code = [data objectForKey:@"partCode"];
                model.version = [data objectForKey:@"partVersion"];
                model.name = [data objectForKey:@"partName"];
                model.number = @"1";;
                
                if (self.boxcode == nil) {
                    self.boxcode =  @"";
                }
                if (self.boxID == nil) {
                    self.boxID = @"";
                }
                if (self.cabinetCode == nil) {
                    self.cabinetCode =  @"";
                }
                [data setObject:@1 forKey:@"number"];
                [data setObject:self.boxcode forKey:@"boxCode"]; //箱号
                [data setObject:self.boxID forKey:@"boxID"]; //箱号ID
                [data setObject:self.cabinetCode forKey:@"cabinetCode"]; //智能柜编号
                [data setObject:self.boxID forKey:@"cargoSpace"]; //货位号
                [data setObject:[NSNumber numberWithInt:[self.type intValue]] forKey:@"partType"];
                
                [self.detailArr addObject:data];
                [self.platesArr addObject:model];
                [[BDHUD sharedManager]dismissHUD];
                // 传送数据
//                CacheVC *vc = [[CacheVC alloc]init];
//                vc.cabinetCode = self.cabinetCode;
//                vc.boxcode = self.boxcode; // 箱号
//                vc.boxID = self.boxID; // 箱号ID
//                vc.freenumber = self.freenumber; // 空余数量
//                vc.serialNumber = self.serialNumber; // 流水号
//                vc.type = self.type;
                [[NSUserDefaults standardUserDefaults]setObject:self.detailArr forKey:@"shoudongDetailArr"];
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            else{
                //                [Toolobject showAlterwithMessage:@"获取板件信息失败，请重新扫描序列号"];
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    //                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                    [Toolobject showAlterwithMessage:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            
                }
                else{
                    [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                }
            }
            [[BDHUD sharedManager]dismissHUD];
        }
        
        else{
            //            [Toolobject showAlterwithMessage:@"获取板件信息失败，请重新扫描序列号"];
            
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                //                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                [Toolobject showAlterwithMessage:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
            [[BDHUD sharedManager]dismissHUD];
            
        }
    } fail:^(id responseObject, NSError *error) {
        
        
        //        [Toolobject showAlterwithMessage:@"获取板件信息失败，请重新扫描序列号"];
        
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
//    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
//    if (![string isEqualToString:filtered]) {
//        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"密码只能输入字母或数字" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
//        [alert show];
//        [textField resignFirstResponder];
//    }
//    return [string isEqualToString:filtered];
//}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
